<?php
namespace app\api\controller\v1;

use app\api\service\TokenService;
use app\api\validate\IDPostiveInt;
use app\api\model\Advert as AdvertModel;
use app\api\model\ArticleType as ArticleTypeModel;
use app\api\model\Banner as BannerModel;
use app\lib\exception\BaseException;
use app\api\validate\AdvertValidate;
use app\api\validate\ActicleTypeValidate;
use app\api\validate\TypeValidate;
use think\Validate;
use think\Db;

//新闻类
class Advert extends Base
{
    //广告位添加编辑
    public function add_advert()
    {
        $validate = new AdvertValidate();
        $validate->goCheck();
        $post = input('post.');//过滤非验证器中的字段
        $post['author'] = TokenService::getAdminName();
        $res = AdvertModel::CreateOne($post);
        if(!$res){
            throw new BaseException(['msg'=>'添加失败']);
        }
        return 1;
    }

    //广告位删除
    public function del_advert($id)
    {
        (new IDPostiveInt)->goCheck();
        Db::startTrans();
        try {
            AdvertModel::destroy($id);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();// 回滚事务
            throw new BaseException(['msg' => '删除失败']);
        }
        return 1;
    }

    //广告位详情
    public function get_advert($id)
    {
        (new IDPostiveInt)->goCheck();
        return AdvertModel::where(['id'=>$id])->find();
    }

    //小程序获取advert_detail
    public function advert_detail($type){
        return AdvertModel::where(['type'=>$type])->find();
    }
}
